<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <script>
        /*逻辑运算符: &&,||,!
              其他类型转为Boolean:
                   1.number: 0或者NAN为false, 其他为true
                   2.String:除了空字符串"",其他都为true
                   3.null和undifined:都是false
                   4.对象: 所有对象都是true
         */

        var flag = true;
        document.write(flag + "<br>");//true
        document.write(!flag + "<br>");//false
        document.write("<hr>");

        //number类型转Boolean
        var a = 3;
        var b = 0;
        var c = NaN;
        document.write(a > b && a > c);//false
        document.write(!!a + "<br>");//true
        document.write(!!b + "<br>");//false
        document.write(!!c + "<br>");//false
        document.write("<hr>");

        //String类型转Boolean
        var str = "abc";
        var str2 = ""
        document.write(!!str + "<br>");//true
        document.write(!!str2 + "<br>");//false
        document.write("<hr>");

        // null和undifined类型转Boolean
        var obj = null;
        var obj2;
        var obj4=undefined;
        document.write(!!obj + "<br>");//false
        document.write(!!obj2 + "<br>");//false
        document.write(!!obj4 + "<br>");//false
        document.write("<hr>");

        //对象类型转Boolean
        var date = new Date();
        document.write(!!date + "<br>")//true

        var obj3 = "123";
        //在java中写法
        if (obj3!=null&& obj3.length>0) {//防止空指针异常
            alert(1234)
        }
       //在js中可以这样定义,简化书写
        if (obj3) {//防止空指针异常
            alert(1111)
        }


    </script>
    <body>

    </body>
</html>